/*	$Id: if.h 7 2008-07-29 02:58:11Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _PFORGE_IF_H_
#define _PFORGE_IF_H_

#include <pforge/pforge.h>
#include <pforge/pkt.h>
#include <pforge/queue.h>
#include <pforge/addr.h>
#include <pforge/dlink.h>


/* Be safe against net/if.h's macro shortcuts */
#ifdef if_type
#undef if_type
#endif
#ifdef if_mtu
#undef if_mtu
#endif


#define PFIF_MAXIF         64   /* maximum interfaces */
#define PFIF_MAXADDR       16

#define PFIF_INDEX_INVAL  (-1)
#define PFIF_INDEX_INIT     1
#define PFIF_INDEX_MAX     (PFIF_MAXIF - 1)
#define PFIF_NAME_MAXLEN   16
#define PFIF_HWADDR_MAXLEN  8


/* Interface Types
 *
 * The following list of Network Interface types was derived from the SNMP
 * ifType MIB specification
 *
 *	http://www.iana.org/assignments/ianaiftype-mib
 */

#define PFIF_TYPE_OTHER          1
#define PFIF_TYPE_1822           2
#define PFIF_TYPE_HDH1822        3
#define PFIF_TYPE_DDNX25         4
#define PFIF_TYPE_X25            5
#define PFIF_TYPE_ETHER          6
#define PFIF_TYPE_ISO88023       7
#define PFIF_TYPE_TKNBUS         8
#define PFIF_TYPE_TKNRING        9
#define PFIF_TYPE_MAN           10
#define PFIF_TYPE_STARLAN       11
#define PFIF_TYPE_PRTN10        12
#define PFIF_TYPE_PRTN80        13
#define PFIF_TYPE_HYPRCHN       14
#define PFIF_TYPE_FDDI          15
#define PFIF_TYPE_LAPB          16
#define PFIF_TYPE_SDLC          17
#define PFIF_TYPE_T1            18
#define PFIF_TYPE_E1            19
#define PFIF_TYPE_BISDN         20
#define PFIF_TYPE_PISDN         21
#define PFIF_TYPE_PTPSERIAL     22
#define PFIF_TYPE_PPP           23
#define PFIF_TYPE_LOOP          24
#define PFIF_TYPE_EON           25
#define PFIF_TYPE_ETH3M         26
#define PFIF_TYPE_NSIP          27
#define PFIF_TYPE_SLIP          28
#define PFIF_TYPE_ULTRA         29
#define PFIF_TYPE_DS3           30
#define PFIF_TYPE_SIP           31
#define PFIF_TYPE_FRMRELAY      32
#define PFIF_TYPE_RS232         33
#define PFIF_TYPE_PARA          34
#define PFIF_TYPE_ARCNET        35
#define PFIF_TYPE_ARCNETPLUS    36
#define PFIF_TYPE_ATM           37
#define PFIF_TYPE_MIOX25        38
#define PFIF_TYPE_SONET         39
#define PFIF_TYPE_X25PLE        40
#define PFIF_TYPE_LLC           41
#define PFIF_TYPE_LOCALTALK     42
#define PFIF_TYPE_SMDSDXI       43
#define PFIF_TYPE_FRMRLSRV      44
#define PFIF_TYPE_V35           45
#define PFIF_TYPE_HSSI          46
#define PFIF_TYPE_HIPPI         47
#define PFIF_TYPE_MODEM         48
#define PFIF_TYPE_AAL5          49
#define PFIF_TYPE_SONETPATH     50
#define PFIF_TYPE_SONETVT       51
#define PFIF_TYPE_SMDSICIP      52
#define PFIF_TYPE_PROPVIRT      53
#define PFIF_TYPE_PROPMUX       54
#define PFIF_TYPE_IEEE80212     55
#define PFIF_TYPE_FIBERCHAN     56
#define PFIF_TYPE_HIPPIIF       57
#define PFIF_TYPE_FRMRLIC       58
#define PFIF_TYPE_AFLANE8023    59
#define PFIF_TYPE_AFLANE8025    60
#define PFIF_TYPE_CCTEMUL       61
#define PFIF_TYPE_FASTETHER     62
#define PFIF_TYPE_ISDN          63
#define PFIF_TYPE_V11           64
#define PFIF_TYPE_V36           65
#define PFIF_TYPE_G703AT64K     66
#define PFIF_TYPE_G703AT2MB     67
#define PFIF_TYPE_QLLC          68
#define PFIF_TYPE_FASTETHERFX   69
#define PFIF_TYPE_CHANNEL       70
#define PFIF_TYPE_IEEE80211     71
#define PFIF_TYPE_IBM370PRCHN   72
#define PFIF_TYPE_ESCON         73
#define PFIF_TYPE_DLSW          74
#define PFIF_TYPE_ISDNS         75
#define PFIF_TYPE_ISDNU         76
#define PFIF_TYPE_LAPD          77
#define PFIF_TYPE_IPSWITCH      78
#define PFIF_TYPE_RSRB          79
#define PFIF_TYPE_ATMLOGICAL    80

#define PFIF_TYPE_ADSL          94
#define PFIF_TYPE_RADSL         95
#define PFIF_TYPE_SDSL          96
#define PFIF_TYPE_VDSL          97
#define PFIF_TYPE_CRFP          98
#define PFIF_TYPE_MYRINET       99
#define PFIF_TYPE_VOICEEM      100
#define PFIF_TYPE_VOICEFXO     101
#define PFIF_TYPE_VOICEFXS     102
#define PFIF_TYPE_VOICEENCAP   103
#define PFIF_TYPE_VOIP         104
#define PFIF_TYPE_ATMDXI       105
#define PFIF_TYPE_ATMFUNI      106
#define PFIF_TYPE_ATMIMA       107

#define PFIF_TYPE_VIPA         112
#define PFIF_TYPE_MPC          113
#define PFIF_TYPE_IPOVERATM    114
#define PFIF_TYPE_ISO88025FBR  115
#define PFIF_TYPE_TDLC         116
#define PFIF_TYPE_GBETHER      117
#define PFIF_TYPE_HDLC         118
#define PFIF_TYPE_LAPF         119
#define PFIF_TYPE_V37          120
#define PFIF_TYPE_X25MLP       121
#define PFIF_TYPE_X25HUNTGRP   122

#define PFIF_TYPE_TUN          131
#define PFIF_TYPE_COFFEE       132
#define PFIF_TYPE_CES          133

#define PFIF_TYPE_USB          160

#define PFIF_TYPE_H323GK       164
#define PFIF_TYPE_H323PROXY    165
#define PFIF_TYPE_MPLS         166

/* interface flags */
#define PFIF_FL_UP        0x0001	/* interface is active */
#define PFIF_FL_RUNNING   0x0002
#define PFIF_FL_BCAST     0x0004	/* interface has broadcast address */
#define PFIF_FL_MCAST     0x0008	/* interface supports multicast */
#define PFIF_FL_LOOPBACK  0x0010	/* loopback interface */
#define PFIF_FL_PROMISC   0x0020	/* promiscuous mode enabled */
#define PFIF_FL_PTP       0x0040	/* Point-to-Point link */
#define PFIF_FL_VIRTUAL   0x0080	/* virtual interface */

#define PFIF_INT_FLAGS    0x007e	/* flags that aren't user-settable */


typedef struct pf_netif {
	char    if_name[PFIF_NAME_MAXLEN];
	int     if_index;
	int     if_type;
	int     if_flags;
	int     if_media;
	short   if_mtu;

	/* hardware address */
	uint8_t if_hwaddr[PFIF_HWADDR_MAXLEN];
	int     if_hwalen;

	PF_ADDR  *if_addrs[PFIF_MAXADDR];
	PF_DLINK *if_dlink;			/* datalink binding */
	void     *if_spec;			/* type-specific data */
} PF_NETIF;


struct pf_netif_info {
	char    ifi_name[PFIF_NAME_MAXLEN];
	int     ifi_index;
	int     ifi_type;
	int     ifi_flags;
	int     ifi_mtu;
	int     ifi_media;

	uint8_t ifi_hwaddr[PFIF_HWADDR_MAXLEN];
	int     ifi_hwalen;
};


struct pf_netif_ops {
	int  (*if_init)   (void);
	void (*if_fini)   (void);
	int  (*if_create) (PF_NETIF *);
	int  (*if_delete) (PF_NETIF *);
	int  (*if_input)  (PF_NETIF *, PF_PKT *);
	int  (*if_output) (PF_NETIF *, PF_PKT *);
	int  (*if_ctl)    (PF_NETIF *);
	void (*if_print)  (PF_NETIF *, PF_PKT *);
};



int       pf_netif_init     (void);
void      pf_netif_cleanup  (void);
PF_NETIF* pf_netif_alloc    (void);
void      pf_netif_free     (PF_NETIF *);
int       pf_netif_create   (int, const char *, int);
int       pf_netif_delete   (int);
int       pf_netif_getindex (const char *);
int       pf_netif_getinfo  (int, struct pf_netif_info *);
int       pf_netif_rmaddr   (int, PF_ADDR *);

int       pf_netif_open      (int);
int       pf_netif_close     (int);
int       pf_netif_sendpkt   (int, PF_PKT *);
PF_PKT*   pf_netif_recvpkt   (int);

/* OS-specific functions */
int       pf_netif_probe    (void);

#endif	/* _PFORGE_IF_H_ */
